﻿using fKanban.Model;
using Beisen.Data;
using fKanban.Utility.ObjectExtend;
using System.Collections.Generic;
namespace fKanban.Provider
{
    public class UserProvider
    {
        public IList<UserEntity> GetUserByPara(UserEntity e)
        {
            Database db = Database.GetDatabase("fkanban");
            return SafeProcedure.ExecuteAndGetInstanceList<UserEntity>(db, "dbo.jUserGetByPara",
            delegate(IParameterSet parameters)
            {
                //parameters.AddWithValue("@Address", e.Address);
                parameters.AddWithValue("@Deportment", (int)e.Deportment);
                parameters.AddWithValue("@Email", e.Email);
                //parameters.AddWithValue("@Extension", e.Extension);

                parameters.AddWithValue("@Id", e.Id);
                parameters.AddWithValue("@Mobile", e.Mobile);
                //parameters.AddWithValue("@Password", e.Password);
                //parameters.AddWithValue("@Telphone", e.Telphone);

                //parameters.AddWithValue("@Username", e.Username);
            },
            delegate(IRecord record, UserEntity objectInstance)
            {
                objectInstance.Address = record.GetOrDefault<string>("Address", string.Empty);
                objectInstance.Deportment = (DeportmentEnum)record.GetOrDefault<int>("Deportment", 0);
                objectInstance.Email = record.GetOrDefault<string>("Email", string.Empty);
                objectInstance.Extension = record.GetOrDefault<int>("Extension", 0);
                objectInstance.Id = record.GetOrDefault<int>("Id", 0);

                objectInstance.Mobile = record.GetOrDefault<string>("Mobile", string.Empty);
                objectInstance.Password = record.GetOrDefault<string>("Password", string.Empty);
                objectInstance.Telphone = record.GetOrDefault<int>("Telphone", 0);
                objectInstance.Username = record.GetOrDefault<string>("Username", string.Empty);
            });
        }

        public int Register(UserEntity e) {
            Database db = Database.GetDatabase("fkanban");
            var identity = SafeProcedure.ExecuteScalar(db, "dbo.jTaskAdd", delegate(IParameterSet parameters)
            {
                parameters.AddWithValue("@Address", e.Address);
                parameters.AddWithValue("@Deportment", e.Deportment);
                parameters.AddWithValue("@Email", e.Email);
                parameters.AddWithValue("@Extension", e.Extension);

                parameters.AddWithValue("@Id", e.Id);
                parameters.AddWithValue("@Mobile", e.Mobile);
                parameters.AddWithValue("@Password", e.Password);
                parameters.AddWithValue("@Telphone", e.Telphone);

                parameters.AddWithValue("@Username", e.Username);
            });
            return identity.ToInt();
        }
        public bool Update(UserEntity userEntity) {
            return false;
        }
    }
}